<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-card>
      <el-card>
        <el-tree
          :data="list"
          node-key="id"
          default-expand-all
          :expand-on-click-node="false"
        >
          <template slot-scope="{ node, data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col @click.native="f(node, data)">{{
                    data.manager
                  }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hDel(data)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit ? '编辑部门' : '添加部门'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="hClose"
    >
      <deptDialog
        ref="refDeptDialog"
        :valid-list="newData"
        :is-edit="isEdit"
        :pid="curId"
        @close="showDialog = false"
        @success="hSuccess"
      />
    </el-dialog>
  </div>
</template>

<script>
import deptDialog from './deptDialog.vue'
import { getTree } from '@/utils/index'
import { getDepartments, delDepartments } from '@/api/department'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      isEdit: '', // 存bool值 false代表添加 true代表编辑
      curId: '',
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      // 提供给子组件做表单验证的数据
      newData: []
    }
  },
  created() {
    // 发送请求获取数据列表
    this.initDepartments()
  },
  methods: {
    // dialog的close回调 关闭弹窗即可删除数据 一劳永逸~~
    hClose() {
      this.$nextTick(() => {
        this.$refs.refDeptDialog.resetFrom()
      })
    },
    // 添加完成， 点确定关闭弹窗
    hSuccess() {
      this.showDialog = false
      // 重新获取数据列表
      this.initDepartments()
    },
    // 编辑
    hEdit(id) {
      // 更新id
      this.curId = id
      // 打开弹窗
      this.showDialog = true
      // 设置编辑状态
      this.isEdit = true
      // $nextTick(回调) 回调里面的代码会等到dom渲染完成之后执行
      this.$nextTick(() => {
        this.$refs.refDeptDialog.initDepartDetail()
      })
    },
    // 点击删除按钮
    hDel(data) {
      if (data.children.length !== 0) { return this.$message.error('不允许删除有子级的部门') }
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          try {
            await delDepartments(data.id)
            // 删除成功重新获取数据列表
            this.initDepartments()
          } catch (err) {
            console.log(err)
          }
        })
        .catch(() => {})
    },
    // 用户点击添加子部门
    hAdd(id) {
      // 设置添加状态
      this.isEdit = false
      // 显示弹框
      this.showDialog = true
      // console.log(data)
      this.curId = id
    },
    f(node, data) {
      console.log(node, data)
    },
    handleNodeClick(data) {
      console.log(data)
    },
    // 发送请求 拿到企业部门列表存data
    async initDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res)
        // 删除数组第一个元素 返回值：被删除的元素
        res.data.depts.shift()
        // 储存列表数据
        this.list = getTree(res.data.depts)
        // 拿出子组件做业务校验需要的数据
        this.newData = res.data.depts.map((ele) => {
          return {
            id: ele.id,
            pid: ele.pid,
            name: ele.name,
            code: ele.code
          }
        })
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>
